package it.polimi.traveldream.web.beans;

import java.util.Date;
import javax.mail.*;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@ManagedBean(name="emailerService")
@Stateless
@LocalBean
public class EmailerService {
	
	@Resource(name = "mail/gmailSession")
	private Session mailSession;

	public void sendEmail(String to, String subject, String message) throws AddressException, MessagingException {
		Message mess = new MimeMessage(mailSession);
		mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
		mess.setSubject(subject);
		mess.setText(message);
		mess.setHeader("TravelDream Mailer", "TravelDream Mailer");
		Date timeStamp = new Date();
		mess.setSentDate(timeStamp);
		Transport.send(mess);
	}
}